package ltd.itlover.yqblog.blog;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.yalantis.ucrop.UCrop;

import dagger.hilt.android.AndroidEntryPoint;
import ltd.itlover.yqblog.R;
import ltd.itlover.yqblog.main.ForumFragment;
import ltd.itlover.yqblog.main.MineFragment;

@AndroidEntryPoint
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private ViewPager2 viewPager2;
    public static Context context;
    private BlogFragment blogFragment;
    private ForumFragment forumFragment;
    private MineFragment mineFragment;
    public static Context getMainContext(){
        return context;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        context = MainActivity.this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RadioGroup radioGroup = findViewById(R.id.bottom_radio_group);
        radioGroup.setOnCheckedChangeListener(this);

        ViewPager2Adapter viewPager2Adapter = new ViewPager2Adapter(this);
        blogFragment = new BlogFragment();
        forumFragment = new ForumFragment();
        mineFragment = new MineFragment();
        viewPager2Adapter.addFragment(blogFragment);
        viewPager2Adapter.addFragment(forumFragment);
        viewPager2Adapter.addFragment(mineFragment);

        viewPager2 = findViewById(R.id.main_view_pager);
        viewPager2.setAdapter(viewPager2Adapter);
        viewPager2.setCurrentItem(0);
        viewPager2.setUserInputEnabled(false);
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position){
                    case 0:{
                        ((RadioButton) findViewById(R.id.radio_button_blog)).setChecked(true);
                        break;
                    }
                    case 1:{
                        ((RadioButton) findViewById(R.id.radio_button_forum)).setChecked(true);
                        break;
                    }
                    case 2:{
                        ((RadioButton) findViewById(R.id.radio_button_mine)).setChecked(true);
                        break;
                    }
                }
            }
        });

    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case R.id.radio_button_blog:{
                viewPager2.setCurrentItem(0);
                break;
            }
            case R.id.radio_button_forum:{
                viewPager2.setCurrentItem(1);
                break;
            }
            case R.id.radio_button_mine:{
                viewPager2.setCurrentItem(2);
                break;
            }
        }
    }

    //在回调中取图片
//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if(requestCode == UCrop.REQUEST_CROP){
//            mineFragment.onActivityResult(requestCode,resultCode, data);
//        }
//
//    }
}